$(function() {
	platformId = "";// 当前页面所使用的平台id
	getPlatformId();//
	//数据初始值
	getEMULocalStorageInit();
	//监听数据变化，存储本地数据
	addListenerEventFun();
	addNumberKeyboard();
});

function getPlatformId() {// 从测试序列界面的链接路径中截取ID
	var url = location.search; // 获取url中"?"符后的字串
	path = location.host;
	if (url.indexOf("?") != -1) {
		var str = url.substr(1);
		strs = str.split("&");
		platformId = strs[3].split("=")[1];
	}
}

tests=function(moudle){ // BTM页面数据发送功能
	var str= "";
	//当前选择的tab索引
	var selectIndex = Number($("#myTab").find('.active').find("a").attr("name").split("_")[1]);
 	var inputs=$("#emutab"+selectIndex+" input[type=number]");//拿到所有的输入框
 	var selects=$("#emutab"+selectIndex+" select");//拿到所有的下拉框
 	var checkboxs=$("#emutab"+selectIndex+" input[type=checkbox]");//拿到所有的输入框
 	str = str+ "{emucheck:"+(selectIndex+1)+",";
	if(moudle=="start"){//点击的是开始和终止按钮
		console.log("开始测试");
		str = str+getDate(inputs,selects,checkboxs);
		ajaxSendEMUInfo(setSendStr(str),1);
	}
	else if(moudle=="stop"){
		console.log("停止测试");
		str=str+getDate(inputs,selects,checkboxs);
		ajaxSendEMUInfo(setSendStr(str),3);
	}
	else if(moudle=="updated"){
		console.log("更新数据");
		str=str+getDate(inputs,selects,checkboxs);
		ajaxSendEMUInfo(setSendStr(str),2);
	}	
	
	console.log(setSendStr(str));
 };
 
 function setSendStr(str)
 {
	 return str.substring(0,str.length-1)+"}";
 }
 
 function getDate(inputs,selects,checkboxs){
	 var str = "";
	for(var i=0;i<inputs.length;i++){//获取Input的键值
     	var input = $(inputs[i]);
     	var key =input.attr("name");
    		var value =input.val();
     	str += key+':'+value+',';
     }
	for(var i=0;i<checkboxs.length;i++){//获取Input的键值
     	var checkbox = $(checkboxs[i]);
     	var key =checkbox.attr("name");
     	var value =false;
     	if(checkbox.get(0).checked){
     		value = true;
     	}else{
     		value = false;
     	}
     	str += key+':'+value+',';
     }
     for(var i=0;i<selects.length;i++){//获取select的键值
     	var select = $(selects[i]);
     	var key =select.attr("name");
		var value =select.find("option:selected").attr("signalValue"); 
 		str += key+':'+value+',';
     }
     return str;
}
 
function ajaxSendEMUInfo(str,sendSignal) {//外围设备信息发送功能
	addLoading();
	$.ajax({
		"url" : "../../../emu/ajaxSendEMUSignal",
		"type" : "post",
		"data" : {
			"sendSignal" : sendSignal,
			"platformId" : platformId,
			"str" : str
		},
		"success" : function(result) {
			removeLoading();
			if (result.flag == 0) {
				console.log("发送成功");
				book('发送成功');
			} else {
				var content=result.errorInfo;
				book(content);
			}
		},
		"error" : function() {
			removeLoading();
			var content="手工测试连接错误！";
			book(content);
		}
	});
}
function addListenerEventFun(){
	//监听数据变化，存储本地数据
	$("input").on('input',function(e)  
	{  
		switch (e.target.name){
			case 'Pressure':
				localStorage.setItem('EMUPressure', e.target.value);
				break;
			case 'ElePressure':
				localStorage.setItem('EMUElePressure', e.target.value);
				break;
			case 'AirPressure':
				localStorage.setItem('EMUAirPressure', e.target.value);
				break;
			default:
				break;
		}
	}); 
	
	//监听数据变化，存储本地数据
	$("select").on('change',function(e){
		switch (e.target.name){
			//EMU诊断数据
			case 'Producer':
				localStorage.setItem('EMUProducer', e.target.selectedIndex);
				break;
			case 'InterType':
				localStorage.setItem('EMUInterType', e.target.selectedIndex);
				break;
			case 'SelBrake':
				localStorage.setItem('EMUSelBrake', e.target.selectedIndex);
				break;
			case 'SelEle':
				localStorage.setItem('EMUSelEle', e.target.selectedIndex);
				break;
			case 'SelAir':
				localStorage.setItem('EMUSelAir', e.target.selectedIndex);
				break;
			case 'SelBackBrake':
				localStorage.setItem('EMUSelBackBrake', e.target.selectedIndex);
				break;
			case 'SelEmrBrake':
				localStorage.setItem('EMUSelEmrBrake', e.target.selectedIndex);
				break;
			default:
				break;
		}
	});
}
//读取存储数据EMU诊断数据
function getEMULocalStorageInit(){
	//EMU诊断数据
	if(localStorage.getItem('EMUPressure')){
		$("input[name='Pressure']").val(localStorage.getItem('EMUPressure'));
	}
	
	if(localStorage.getItem('EMUElePressure')){
		$("input[name='ElePressure']").val(localStorage.getItem('EMUElePressure'));
	}
	
	if(localStorage.getItem('EMUAirPressure')){
		$("input[name='AirPressure']").val(localStorage.getItem('EMUAirPressure'));
	}
	
	if(localStorage.getItem('EMUProducer')){
		$("select[name = 'Producer']").get(0).selectedIndex = localStorage.getItem('EMUProducer');
	}
	if(localStorage.getItem('EMUInterType')){
		$("select[name = 'InterType']").get(0).selectedIndex = localStorage.getItem('EMUInterType');
	}
	if(localStorage.getItem('EMUSelBrake')){
		$("select[name = 'SelBrake']").get(0).selectedIndex = localStorage.getItem('EMUSelBrake');
	}
	if(localStorage.getItem('EMUSelEle')){
		$("select[name = 'SelEle']").get(0).selectedIndex = localStorage.getItem('EMUSelEle');
	}
	if(localStorage.getItem('EMUSelAir')){
		$("select[name = 'SelAir']").get(0).selectedIndex = localStorage.getItem('EMUSelAir');
	}
	if(localStorage.getItem('EMUSelBackBrake')){
		$("select[name = 'SelBackBrake']").get(0).selectedIndex = localStorage.getItem('EMUSelBackBrake');
	}
	if(localStorage.getItem('EMUCSelEmrBrake')){
		$("select[name = 'SelEmrBrake']").get(0).selectedIndex = localStorage.getItem('EMUCSelEmrBrake');
	}
}

//添加数字软键盘
function addNumberKeyboard(){
	if($(".numkeyboard").length>0){
		$(".numkeyboard").numkeyboard({
			keyboardRadix: 600, //键盘大小基数
			mainbackground: '#313439', //主背景色
			menubackground: '#313439', //头背景色
			exitbackground: '#4376A0', //关闭按钮背景色
			buttonbackground: 'transparent', //键盘背景色
			clickeve: true, //是否绑定元素click事件
		});
	}
}